package org.hpin.asset.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hpin.asset.entity.Assetinfo;
import org.hpin.asset.entity.Assetmaintaininfo;
import org.hpin.asset.service.AssetinfoService;
import org.hpin.asset.service.AssetmaintaininfoService;
import org.hpin.bg.system.util.LogTool;
import org.hpin.common.core.SpringTool;
import org.hpin.common.core.web.BaseAction;
import org.hpin.common.util.HttpTool;
import org.hpin.common.util.StrUtils;
import org.hpin.common.widget.pagination.Page;
/**
 * 
 * @author Marver
 *2015-7-2下午6:19:50
 */

@Namespace("/AssetMInfo")
@Action("assetmaintaininfoAction")
@Results({
	@Result(name="listAssetMinfo" ,location = "/WEB-INF/assetmaintaininfo/listAssetMinfo.jsp"),
	@Result(name="addAssetMinfo" ,location = "/WEB-INF/assetmaintaininfo/addAssetMinfo.jsp"),
	@Result(name="browAssetMinfo", location = "/WEB-INF/assetmaintaininfo/browAssetMinfo.jsp"),
	@Result(name="modifyAssetMinfo", location = "/WEB-INF/assetmaintaininfo/modifyAssetMinfo.jsp")
	})
public class AssetmaintaininfoAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3652933813692432106L;
	private Assetmaintaininfo assetmaintaininfo;
	private Assetinfo assetinfo;
	private AssetmaintaininfoService assetmaintaininfoService=(AssetmaintaininfoService) SpringTool.getBean(AssetmaintaininfoService.class);
	private AssetinfoService assetinfoService=(AssetinfoService) SpringTool.getBean(AssetinfoService.class);
	private AssetinfoAction assetinfoAction =new AssetinfoAction();
	
	/**
	 * 资产维修查询
	 * @return
	 */
	public String listAssetMinfo() throws Exception{
		page = new Page(HttpTool.getPageNum(),HttpTool.getPageSize());
		Map searchMap = super.buildSearch();
		findByPage(page, searchMap);
		return "listAssetMinfo";
	}
	public Page findByPage(Page page , Map searchMap){
		assetmaintaininfoService.findByPage(page, searchMap);
		return page ;
	}
	
	/**
	 * 查询资产维修详细信息
	 * @return
	 */
	public String browAssetMinfo(){
		assetmaintaininfo=(Assetmaintaininfo) assetmaintaininfoService.findById(id);
		String id=assetmaintaininfo.getAssetno();
		assetinfo=(Assetinfo) assetinfoService.findById(id);
		return "browAssetMinfo";
	}
	
	public String addAssetMinfo() throws Exception{
		HttpTool.setAttribute("navTabId", HttpTool.getParameter("navTabId"));
		
		if(StrUtils.isNotNullOrBlank(id)){
			assetmaintaininfo=(Assetmaintaininfo) assetmaintaininfoService.findById(id);
			HttpTool.setAttribute("id", id);
		}else{
			assetmaintaininfo = new Assetmaintaininfo();
			page = new Page(HttpTool.getPageNum(),HttpTool.getPageSize());
			Map searchMap = super.buildSearch();
			searchMap.put("filter_and_state_NEQ_S", "1010104");
			searchMap.put("filter_and_state_NNEQ_S", "1010103");
			searchMap.put("filter_and_isDeleted_EQ_I",0);
			assetinfoAction.findByPage(page, searchMap);
		}
		return "addAssetMinfo";
	}
	
	//添加资产维护信息保存到数据库
		public String saveAssetMinfo(){
			JSONObject json = new JSONObject();
			try {
				String currentTabId = HttpTool.getParameter("currentTabId");
				assetmaintaininfo.setRepairOrder(randomNum());
				assetmaintaininfo.setAssetno(id);
				
				assetinfo=(Assetinfo) assetinfoService.findById(id);
				HttpTool.getRequest().setAttribute("assetInfo", assetinfo);
				String state=HttpTool.getParameter("state");
				assetinfo.setState(state);
				assetmaintaininfo.setState(state);
				assetmaintaininfoService.update(assetinfo);
				
				assetmaintaininfoService.save(assetmaintaininfo);
				HttpTool.getRequest().setAttribute("assetMinfo", assetmaintaininfo);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
				json.accumulate("callbackType", "closeCurrent");
			} catch (Exception e) {
				json.accumulate("statusCode", 300);
				json.accumulate("message", "操作失败");
				e.printStackTrace();
			}
			renderJson(json);
			return null;
		}
	
		/**
		 * 修改前查询维修资产信息
		 * @return
		 */
		public String modifyAssetMinfo(){
			assetmaintaininfo=(Assetmaintaininfo) assetmaintaininfoService.findById(id);
			String id=assetmaintaininfo.getAssetno();
			assetinfo=(Assetinfo) assetinfoService.findById(id);
			return "modifyAssetMinfo";
		}
		
		/**
		 * 修改资产维护信息
		 * @return
		 */
		public String updateAssetMinfo(){
			JSONObject json = new JSONObject();
			try {
				assetinfo=(Assetinfo) assetinfoService.findById(assetmaintaininfo.getAssetno());
				HttpTool.getRequest().setAttribute("assetInfo", assetinfo);
				assetinfo.setState(assetmaintaininfo.getState());
				assetinfoService.update(assetinfo);
				
				assetmaintaininfoService.update(assetmaintaininfo);
				HttpTool.getRequest().setAttribute("assetMinfo", assetmaintaininfo);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("callbackType", "closeCurrent");
			} catch (Exception e) {
				json.accumulate("statusCode", 300);
				json.accumulate("message", "操作失败");
				e.printStackTrace();
			}
			renderJson(json);
			return null;
		}
		
		/**
		 * 删除维修信息
		 * @return
		 */
		public String deleteAssetMinfo(){
			JSONObject json = new JSONObject();
			try {
				String[] id = ids.replaceAll(" ", "").split(",");
				
				assetmaintaininfoService.deleteMSG(id);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("callbackType", "");
				json.accumulate("forwardUrl", "");
				json.accumulate("confirmMsg", "");
			} catch (Exception e) {
				json.accumulate("statusCode", 300);
				json.accumulate("message", "操作失败");
				e.printStackTrace();
			}
			renderJson(json);
			return null;
		}
		
	public Assetmaintaininfo getAssetmaintaininfo() {
		return assetmaintaininfo;
	}
	public void setAssetmaintaininfo(Assetmaintaininfo assetmaintaininfo) {
		this.assetmaintaininfo = assetmaintaininfo;
	}
	
	
	public Assetinfo getAssetinfo() {
		return assetinfo;
	}
	public void setAssetinfo(Assetinfo assetinfo) {
		this.assetinfo = assetinfo;
	}
	
	public static String randomNum(){
		SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
		String a=df.format(new Date());
		int b=(int)((Math.random()*9+1)*100);
		String num=a+b;
		return num;
	}
	
	
	public static void main(String[] args) {
		System.out.println("('1010104','1010103')");
	}
	
}
